package com.i8office.android.zhihuixinwu.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cyan.tools.annotation.ActivityFragmentInject;
import com.cyan.tools.base.BaseFragment;
import com.cyan.tools.base.RecyclerArrayAdapter;
import com.cyan.tools.util.RxBus;
import com.i8office.android.zhihuixinwu.widget.ItemDecoration;
import com.i8office.android.zhihuixinwu.R;
import com.i8office.android.zhihuixinwu.adapter.OpinionAdapter;
import com.i8office.android.zhihuixinwu.module.contract.OpinionContract;
import com.i8office.android.zhihuixinwu.module.presenter.OpinionPresenter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;


/**
 * Created by pc on 2018/7/7.
 */
@ActivityFragmentInject(
        contentViewId = R.layout.fragment_opinion
)
public class OpinionFragment extends BaseFragment<OpinionContract.Presenter> implements OpinionContract.View {
    @BindView(R.id.list)
    RecyclerView recyclerView;
    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    @BindView(R.id.empty)
    TextView empty;
    @BindView(R.id.error)
    TextView error;
    private OpinionAdapter adapter;
    private Observable<Boolean> observable;
    private Disposable disposable;
    public boolean is_first = true;
    @Override
    protected void initView(View rootView) {
        ButterKnife.bind(this, rootView);
        mPresenter = new OpinionPresenter(this);
        adapter = new OpinionAdapter(getActivity());
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                RxBus.get().post("add_opinion",adapter.getItem(position));
            }
        });
        refreshLayout.setColorSchemeColors(ContextCompat.getColor(getActivity(),R.color.tab_top_bg));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new ItemDecoration(getActivity(), 1));
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                fetch();
            }
        });
       fetch();
       if(getArguments().getBoolean("is_private",false))
       {observable= RxBus.get().register("add_succeed",Boolean.class);
        disposable=observable.subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean s) throws Exception {
                fetch();
            }
        });}
    }
    public void fetch(){
        if(getArguments().getBoolean("is_private",false))
            mPresenter.getPrivateOpinion();
        else mPresenter.getPublicOpinion();
    }

    @Override
    public void showResult(ArrayList<String> data) {
        adapter.clear();
        error.setVisibility(View.GONE);
        empty.setVisibility(View.GONE);
        adapter.addAll(data);
    }

    @Override
    public void showEmpty() {
            empty.setVisibility(View.VISIBLE);
    }

    @Override
    public void showError() {
            error.setVisibility(View.VISIBLE);
    }

    @Override
    public void showProgress() {
            error.setVisibility(View.GONE);
            empty.setVisibility(View.GONE);
            if(is_first)
            {progressBar.setVisibility(View.VISIBLE);
            is_first=false;}
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
        refreshLayout.setRefreshing(false);
    }
    @OnClick(R.id.error)
    public void onViewClicked() {
      fetch();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(disposable!=null&&!disposable.isDisposed())disposable.dispose();
        if(observable!=null)RxBus.get().unregister("add_succeed",observable);
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("is_first", is_first);
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (savedInstanceState != null) {
            is_first = savedInstanceState.getBoolean("is_first");
        }
    }
}
